package cc.rengu.igas.mcps.core.service.trans.scanpay;

import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.service.base.C2BAuthService;
import cc.rengu.igas.mcps.facade.request.PosScanQrCodePreAuthReversalRequest;
import cc.rengu.igas.mcps.facade.response.PosScanQrCodePreAuthReversalResponse;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.oltp.service.base.ChannelBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.*;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.utility.util.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * POS条码预授权冲正服务.
 */
public class PosScanQrCodePreAuthRevsalService extends C2BAuthService implements TransCustomImpl {

    @Override
    protected Object bizBeforeCust() throws Exception {
        PosScanQrCodePreAuthReversalRequest posScanQrCodePreAuthReversalRequest = new PosScanQrCodePreAuthReversalRequest();
        ConvertUtil.convertOutput(posScanQrCodePreAuthReversalRequest);
        return posScanQrCodePreAuthReversalRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        PosScanQrCodePreAuthReversalRequest posScanQrCodePreAuthReversalRequest = (PosScanQrCodePreAuthReversalRequest) request;

        /* 获取原订单信息 */
        UserOrderInfo userOrderInfo = (UserOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_USER_ORDER_INFO);
        MchntOrderInfo mchntOrderInfo = (MchntOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_MCHNT_ORDER_INFO);
        PayOrderInfo payOrderInfo = (PayOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_PAY_ORDER_INFO);

        /*初始化订单服务*/
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserOrderInfo(userOrderInfo);
        List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
        mchntOrderInfoList.add(mchntOrderInfo);
        orderInfo.setMchntOrderInfoList(mchntOrderInfoList);
        List<PayOrderInfo> payOrderInfoList = new ArrayList<>();
        payOrderInfoList.add(payOrderInfo);
        orderInfo.setPayOrderInfoList(payOrderInfoList);

        /* 调用通道接口 */
        if (null == txnInfo.getChannelCallCfgList() || txnInfo.getChannelCallCfgList().isEmpty()) {
            rglog.error("<{}-{}>交易未配置交易通道服务调用配置信息！", txnInfo.getSrcTxnAuthCfg().getTxnNum(), txnInfo.getSrcTxnAuthCfg().getTxnName());
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }
        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item ->
                item.getCallIndex().equals(ChannelBaseService.FIRST_CHANNEL_CALL_INDEX)).collect(Collectors.toList());
        ChannelCallCfg channelCallCfg = channelCallSceneDiscern(posScanQrCodePreAuthReversalRequest, channelCallCfgList);
        Class channelClasz = Class.forName(channelCallCfg.getCallChannelImpl());
        ChannelBaseService channelBaseService = (ChannelBaseService) channelClasz.newInstance();
        channelBaseService.callService(txnInfo, orderInfo, channelCallCfg.getBizScene(), request);

        /* 组装应答报文 */
        PosScanQrCodePreAuthReversalResponse posScanQrCodePreAuthReversalResponse = new PosScanQrCodePreAuthReversalResponse();
        posScanQrCodePreAuthReversalResponse.setHeader(posScanQrCodePreAuthReversalRequest.getHeader());
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(posScanQrCodePreAuthReversalResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        PosScanQrCodePreAuthReversalResponse PosScanQrCodePreAuthReversalResponse = (PosScanQrCodePreAuthReversalResponse) bizResponse.getResult();
        ConvertUtil.convertInput(PosScanQrCodePreAuthReversalResponse);
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        PosScanQrCodePreAuthReversalRequest posScanQrCodePreAuthReversalRequest = (PosScanQrCodePreAuthReversalRequest) request;
        /* 业务细分：bizType：00-微信条码预授权冲正(暂不支持)，01-支付宝条码预授权冲正，02-银联二维码条码预授权冲正 */
        String bizType = posScanQrCodePreAuthReversalRequest.getHeader().getBizType();
        Optional<ChannelCallCfg> channelCallCfgOpt = channelCallCfgList.stream().filter(item ->
                item.getBizScene().equals(bizType)).findFirst();
        return channelCallCfgOpt.orElse(null);
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        return CheckResultEnum.PASSED;
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        return transStatusEnum;
    }
}
